<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of areaAction
 *
 * @author xlp
 */
class areaAction extends commonAction {

    protected $level; //area_level 区域级别 0:社区 1:街道 2:城区

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
        $this->level = array(1 => '城区', 2 => '街道', 3 => '社区', 4 => '小区');
        $this->typeLevel = array(0, 1, 2);
    }

    function index() {
        $q = $this->_get('q');
        $fid = $this->_getid('fid', 0);
        $level = $this->_getid('level', 1); //当前级别 1：城区 2：街道 3：社区 4:小区
        $p = $this->_getid('p', 1);
        $tlevel = $level;
        if (!in_array(($level - 1), $this->typeLevel)) {
            $level = 1;
        }
        if ($level==1){
//            $fid=0;
        }
        $nextLevel = $level + 1;
        $next = isset($this->level[$nextLevel]) ? array('type' => $nextLevel, 'name' => $this->level[$nextLevel]) : array('type' => 1, 'name' => $this->level[1]);
        $preLevel = $level - 1;
        $pre = isset($this->level[$preLevel]) ? array('type' => $preLevel, 'name' => $this->level[$preLevel]) : array('type' => 1, 'name' => $this->level[1]);
        if ($tlevel == 4) {
            $where = array('community_id' => $fid, 'city_id' => admin::$adminInfo['city_id']);
            $rs = M('village')->where($where)->page($p)->order('vid DESC')->findAll();
            $total = M('village')->getTotal();
        } else {
            $where = array('AND' => array('fid' => $fid, 'city_id' => admin::$adminInfo['city_id']));
            if ($q) {
                $where['LIKE'] = array('name' => parent::safeSearch($q));
            }
            $rs = M('area')->where($where)->page($p)->order('aid DESC')->findAll();
            $total = M('area')->getTotal();
//        z(M('area')->getAllSql());
        }
        if ($rs) {
            foreach ($rs as $k => $v) {
                $rs[$k]['city_name'] = parent::getCityName($v['city_id']);
            }
        }
        $this->assign(array('rs' => $rs, 'total' => $total, 'p' => $p, 'fid' => $fid, 'level' => $this->level, 'next' => $next, 'myLevel' => $level, 'pre' => $pre));
//        print_r($this->getAllVar());
        if ($tlevel == 4) {
            $this->display('village');
        } else {
            $this->display();
        }
    }

    function add() {
        $fid = $this->_getid('fid', 0);
        $level = $this->_getid('level', 1);
        $this->assign(array('level' => $level, 'fid' => $fid));
        $this->display();
    }

    function post() {
        $objData = array();
        $objData['city_id'] = admin::$adminInfo['city_id'];
        $objData['fid'] = $this->_postid('fid', 0);
        $objData['area_level'] = $this->_postid('level', 1);
        $objData['name'] = $this->_post('name');
        $objData['lat'] = $this->_post('lat', 0);
        $objData['lng'] = $this->_post('lng', 0);
        $objData['zoom'] = $this->_post('zoom', 0);
        switch ($objData['area_level']) {
            case 0:
            case 1:
                $objData['area_level'] = 2;
                break;
            case 2:
                $objData['area_level'] = 1;
                break;
            case 3:
                $objData['area_level'] = 0;
                break;
            default:
                $objData['area_level'] = 2;
        }
        //判断父级是否存在
        if ($objData['fid'] && !M('area')->where(array('aid' => $objData['fid'], 'city_id' => $objData['city_id']))->find()) {
            $this->JsonReturn('上级区域不存在，请检查');
        }
        //判断重复
        if (M('area')->where(array('name' => $objData['name'], 'city_id' => $objData['city_id'], 'fid' => $objData['fid']))->find()) {
            $this->JsonReturn('同名区域已存在，请检查');
        }
        if (!in_array($objData['area_level'], $this->typeLevel)) {
            $this->JsonReturn('区域级别不正确，请检查' . $objData['area_level']);
        }
//        z($objData);
        M('area')->insert($objData);
        $this->JsonReturn('ok', null, 1);
    }

    function edit() {
        $id = $this->_getid('id', 0);
        if (!$id) {
            showError('参数丢失');
        }
        $rs = M('area')->where(array('aid' => $id))->find();
        if (!$rs) {
            showError('区域不存在');
        }
//        z($rs);
        $this->assign(array('rs' => $rs));
        $this->display();
    }

    function save() {
        $id = $this->_postid('id', 0);
        if (!$id) {
            showError('参数丢失');
        }
        $rs = M('area')->where(array('aid' => $id))->find();
        if (!$rs) {
            showError('区域不存在');
        }
        $objData = array();
        $objData['name'] = $this->_post('name');

        //判断重复
        if (M('area')->where(array('name' => $objData['name'], 'city_id' => $rs['city_id'], 'fid' => $rs['fid']))->find()) {
            $this->JsonReturn('同名区域已存在，请检查');
        }
//        z($objData);
        M('area')->update($objData, array('aid' => $id));
        $this->JsonReturn('ok', null, 1);
    }

    function delete() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        D('area')->delAreaInfo($id);
        $this->JsonReturn('ok', null, 1);
    }

    function search() {
        $type = $this->_getid('type', 0);
        $field = $this->_getid('field', 0);
        $fid = $this->_getid('fid', 0);
        $this->assign(array('type' => $type, 'field' => $field, 'fid' => $fid));
        $this->display();
    }

    function ajax() {
        $title = $this->_get('title');
        $type = $this->_getid('type', 0);
        $fid = $this->_getid('fid', 0);
        $p = $this->_getid('p', 1);
        $where = array('LIKE' => array('name' => parent::safeSearch($title)), 'AND' => array('area_level' => $type, 'city_id' => admin::$adminInfo['city_id']));
        if (!$title) {
            showError('请输入关键字');
        }
        if ($fid) {
            $where['AND']['fid'] = $fid;
        }
        $rs = M('area')->where($where)->page($p, 5)->findAll();
//        z(M('area')->getAllSql());
        $this->assign(array('rs' => $rs, 'total' => M('area')->getTotal(), 'p' => $p));
        $this->JsonReturn('ok', $this->getFetch(), 1);
    }

}
